<?php

namespace addons\yunfood\model;

use addons\yunfood\library\ModelTrait;
use addons\yunfood\library\Super;
use think\Model;
use traits\model\SoftDelete;

/**
 * 储值套餐模型
 */
class FundPack extends Model
{
    use ModelTrait;
    use SoftDelete;

    // 表名,不含前缀
    protected $name = 'yunfood_fund_pack';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $deleteTime = 'deleted_at';
    protected $updateTime = 'updated_at';
    protected $createTime = 'created_at';

    // 不显示的字段
    protected $hidden = ['deleted_at'];

    protected $resultSetType = 'collection';

    // 状态
    const STATUS_NORMAL = 1;
    const STATUS_HIDDEN = 2;

    public static function getStatusList()
    {
        return [self::STATUS_NORMAL => __('Normal'), self::STATUS_HIDDEN => __('屏蔽')];
    }

    public function getStatusTextAttr($value, $data)
    {
        $optionList = self::getStatusList();

        return $optionList[$data['status']] ?? $data['status'];
    }

    public function getNameTextAttr($value, $data)
    {
        if ($data['name']) {
            return $data['name'];
        }
        if ($data['price'] != $data['amount']) {
            return '充' . (float)$data['price'] . '得' . (float)$data['amount'];
        }

        return '充' . (float)$data['price'];
    }

    // 获取select列表
    public static function getOptionList()
    {
        $dataList = FundPack::select();
        $dataList = Super::arrayGetSet($dataList, ['id', 'name' => 'name_text'], true);

        return array_column($dataList, 'name', 'id');
    }
}
